<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="assetId">
        <el-input
          v-model="queryParams.assetId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备件ID" prop="spareId">
        <el-input
          v-model="queryParams.spareId"
          placeholder="请输入备件ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初始日期" prop="initialDate">
        <el-date-picker clearable
          v-model="queryParams.initialDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择初始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分数" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入分数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="等级" prop="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="STANDBY 备用评分" prop="standby">
        <el-input
          v-model="queryParams.standby"
          placeholder="请输入STANDBY 备用评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="QUALITY 质量评分" prop="quality">
        <el-input
          v-model="queryParams.quality"
          placeholder="请输入QUALITY 质量评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SAFETY/HEALTH 安全健康评分" prop="safetyHealth">
        <el-input
          v-model="queryParams.safetyHealth"
          placeholder="请输入SAFETY/HEALTH 安全健康评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ENVIRONMENT 环境评分" prop="environment">
        <el-input
          v-model="queryParams.environment"
          placeholder="请输入ENVIRONMENT 环境评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IMPACT 影响评分" prop="impact">
        <el-input
          v-model="queryParams.impact"
          placeholder="请输入IMPACT 影响评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CRITICAL LEVEL 关键等级" prop="criticalLevel">
        <el-input
          v-model="queryParams.criticalLevel"
          placeholder="请输入CRITICAL LEVEL 关键等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="MTBF 平均故障间隔时间" prop="mtbf">
        <el-input
          v-model="queryParams.mtbf"
          placeholder="请输入MTBF 平均故障间隔时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="MTTR 平均修复时间" prop="mttr">
        <el-input
          v-model="queryParams.mttr"
          placeholder="请输入MTTR 平均修复时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="COST 成本" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入COST 成本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SEVERITY 严重性评分" prop="severity">
        <el-input
          v-model="queryParams.severity"
          placeholder="请输入SEVERITY 严重性评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SEVERITY LEVEL 严重性等级" prop="severityLevel">
        <el-input
          v-model="queryParams.severityLevel"
          placeholder="请输入SEVERITY LEVEL 严重性等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="TOTAL FACTOR SCORE 总因子评分" prop="totalFactorScore">
        <el-input
          v-model="queryParams.totalFactorScore"
          placeholder="请输入TOTAL FACTOR SCORE 总因子评分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="EQUIPMENT RISK LEVEL 设备风险等级" prop="equipmentriskLevel">
        <el-input
          v-model="queryParams.equipmentriskLevel"
          placeholder="请输入EQUIPMENT RISK LEVEL 设备风险等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否处理 0 未处理 1已处理" prop="isDeal">
        <el-input
          v-model="queryParams.isDeal"
          placeholder="请输入是否处理 0 未处理 1已处理"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0正常 1删除" prop="isDelete">
        <el-select v-model="queryParams.isDelete" placeholder="请选择0正常 1删除" clearable>
          <el-option
            v-for="dict in asset_delete"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sbtz:assessment:add']"
        >新增</el-button>
      </el-col>
      <el-button type="primary" @click="openAdd">新增评估</el-button>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sbtz:assessment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sbtz:assessment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['sbtz:assessment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assessmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自增主键" align="center" prop="id" />
      <el-table-column label="设备ID" align="center" prop="assetId" />
      <el-table-column label="备件ID" align="center" prop="spareId" />
      <el-table-column label="初始日期" align="center" prop="initialDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.initialDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分数" align="center" prop="score" />
      <el-table-column label="等级" align="center" prop="grade" />
      <el-table-column label="STANDBY 备用评分" align="center" prop="standby" />
      <el-table-column label="QUALITY 质量评分" align="center" prop="quality" />
      <el-table-column label="SAFETY/HEALTH 安全健康评分" align="center" prop="safetyHealth" />
      <el-table-column label="ENVIRONMENT 环境评分" align="center" prop="environment" />
      <el-table-column label="IMPACT 影响评分" align="center" prop="impact" />
      <el-table-column label="CRITICAL LEVEL 关键等级" align="center" prop="criticalLevel" />
      <el-table-column label="MTBF 平均故障间隔时间" align="center" prop="mtbf" />
      <el-table-column label="MTTR 平均修复时间" align="center" prop="mttr" />
      <el-table-column label="COST 成本" align="center" prop="cost" />
      <el-table-column label="SEVERITY 严重性评分" align="center" prop="severity" />
      <el-table-column label="SEVERITY LEVEL 严重性等级" align="center" prop="severityLevel" />
      <el-table-column label="TOTAL FACTOR SCORE 总因子评分" align="center" prop="totalFactorScore" />
      <el-table-column label="EQUIPMENT RISK LEVEL 设备风险等级" align="center" prop="equipmentriskLevel" />
      <el-table-column label="是否处理 0 未处理 1已处理" align="center" prop="isDeal" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="0正常 1删除" align="center" prop="isDelete">
        <template #default="scope">
          <dict-tag :options="asset_delete" :value="scope.row.isDelete"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sbtz:assessment:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sbtz:assessment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <AssessmentDialog
    v-model:visible="dialogVisible"
    :mode="dialogMode"
    :rowData="currentRow"
  />
    <!-- 添加或修改设备评估对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="assessmentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备ID" prop="assetId">
          <el-input v-model="form.assetId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="备件ID" prop="spareId">
          <el-input v-model="form.spareId" placeholder="请输入备件ID" />
        </el-form-item>
        <el-form-item label="初始日期" prop="initialDate">
          <el-date-picker clearable
            v-model="form.initialDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择初始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入分数" />
        </el-form-item>
        <el-form-item label="等级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入等级" />
        </el-form-item>
        <el-form-item label="STANDBY 备用评分" prop="standby">
          <el-input v-model="form.standby" placeholder="请输入STANDBY 备用评分" />
        </el-form-item>
        <el-form-item label="QUALITY 质量评分" prop="quality">
          <el-input v-model="form.quality" placeholder="请输入QUALITY 质量评分" />
        </el-form-item>
        <el-form-item label="SAFETY/HEALTH 安全健康评分" prop="safetyHealth">
          <el-input v-model="form.safetyHealth" placeholder="请输入SAFETY/HEALTH 安全健康评分" />
        </el-form-item>
        <el-form-item label="ENVIRONMENT 环境评分" prop="environment">
          <el-input v-model="form.environment" placeholder="请输入ENVIRONMENT 环境评分" />
        </el-form-item>
        <el-form-item label="IMPACT 影响评分" prop="impact">
          <el-input v-model="form.impact" placeholder="请输入IMPACT 影响评分" />
        </el-form-item>
        <el-form-item label="CRITICAL LEVEL 关键等级" prop="criticalLevel">
          <el-input v-model="form.criticalLevel" placeholder="请输入CRITICAL LEVEL 关键等级" />
        </el-form-item>
        <el-form-item label="MTBF 平均故障间隔时间" prop="mtbf">
          <el-input v-model="form.mtbf" placeholder="请输入MTBF 平均故障间隔时间" />
        </el-form-item>
        <el-form-item label="MTTR 平均修复时间" prop="mttr">
          <el-input v-model="form.mttr" placeholder="请输入MTTR 平均修复时间" />
        </el-form-item>
        <el-form-item label="COST 成本" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入COST 成本" />
        </el-form-item>
        <el-form-item label="SEVERITY 严重性评分" prop="severity">
          <el-input v-model="form.severity" placeholder="请输入SEVERITY 严重性评分" />
        </el-form-item>
        <el-form-item label="SEVERITY LEVEL 严重性等级" prop="severityLevel">
          <el-input v-model="form.severityLevel" placeholder="请输入SEVERITY LEVEL 严重性等级" />
        </el-form-item>
        <el-form-item label="TOTAL FACTOR SCORE 总因子评分" prop="totalFactorScore">
          <el-input v-model="form.totalFactorScore" placeholder="请输入TOTAL FACTOR SCORE 总因子评分" />
        </el-form-item>
        <el-form-item label="EQUIPMENT RISK LEVEL 设备风险等级" prop="equipmentriskLevel">
          <el-input v-model="form.equipmentriskLevel" placeholder="请输入EQUIPMENT RISK LEVEL 设备风险等级" />
        </el-form-item>
        <el-form-item label="是否处理 0 未处理 1已处理" prop="isDeal">
          <el-input v-model="form.isDeal" placeholder="请输入是否处理 0 未处理 1已处理" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="0正常 1删除" prop="isDelete">
          <el-select v-model="form.isDelete" placeholder="请选择0正常 1删除">
            <el-option
              v-for="dict in asset_delete"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Assessment">
import { listAssessment, getAssessment, delAssessment, addAssessment, updateAssessment } from "@/api/sbtz/assessment";

const { proxy } = getCurrentInstance();
const { asset_delete } = proxy.useDict('asset_delete');

const assessmentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
import AssessmentDialog from '@/components/sbtz/assessment/AssessmentDialog.vue'

const dialogVisible = ref(false)
const dialogMode = ref('add')
const currentRow = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    assetId: null,
    spareId: null,
    initialDate: null,
    score: null,
    grade: null,
    standby: null,
    quality: null,
    safetyHealth: null,
    environment: null,
    impact: null,
    criticalLevel: null,
    mtbf: null,
    mttr: null,
    cost: null,
    severity: null,
    severityLevel: null,
    totalFactorScore: null,
    equipmentriskLevel: null,
    isDeal: null,
    isDelete: null
  },
  rules: {
    assetId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    spareId: [
      { required: true, message: "备件ID不能为空", trigger: "blur" }
    ],
    initialDate: [
      { required: true, message: "初始日期不能为空", trigger: "blur" }
    ],
    score: [
      { required: true, message: "分数不能为空", trigger: "blur" }
    ],
    grade: [
      { required: true, message: "等级不能为空", trigger: "blur" }
    ],
    standby: [
      { required: true, message: "STANDBY 备用评分不能为空", trigger: "blur" }
    ],
    quality: [
      { required: true, message: "QUALITY 质量评分不能为空", trigger: "blur" }
    ],
    safetyHealth: [
      { required: true, message: "SAFETY/HEALTH 安全健康评分不能为空", trigger: "blur" }
    ],
    environment: [
      { required: true, message: "ENVIRONMENT 环境评分不能为空", trigger: "blur" }
    ],
    impact: [
      { required: true, message: "IMPACT 影响评分不能为空", trigger: "blur" }
    ],
    criticalLevel: [
      { required: true, message: "CRITICAL LEVEL 关键等级不能为空", trigger: "blur" }
    ],
    mtbf: [
      { required: true, message: "MTBF 平均故障间隔时间不能为空", trigger: "blur" }
    ],
    mttr: [
      { required: true, message: "MTTR 平均修复时间不能为空", trigger: "blur" }
    ],
    cost: [
      { required: true, message: "COST 成本不能为空", trigger: "blur" }
    ],
    severity: [
      { required: true, message: "SEVERITY 严重性评分不能为空", trigger: "blur" }
    ],
    severityLevel: [
      { required: true, message: "SEVERITY LEVEL 严重性等级不能为空", trigger: "blur" }
    ],
    totalFactorScore: [
      { required: true, message: "TOTAL FACTOR SCORE 总因子评分不能为空", trigger: "blur" }
    ],
    equipmentriskLevel: [
      { required: true, message: "EQUIPMENT RISK LEVEL 设备风险等级不能为空", trigger: "blur" }
    ],
    isDeal: [
      { required: true, message: "是否处理 0 未处理 1已处理不能为空", trigger: "blur" }
    ],
    createBy: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateBy: [
      { required: true, message: "更新人不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    isDelete: [
      { required: true, message: "0正常 1删除不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备评估列表 */
function getList() {
  loading.value = true;
  listAssessment(queryParams.value).then(response => {
    assessmentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    assetId: null,
    spareId: null,
    initialDate: null,
    score: null,
    grade: null,
    standby: null,
    quality: null,
    safetyHealth: null,
    environment: null,
    impact: null,
    criticalLevel: null,
    mtbf: null,
    mttr: null,
    cost: null,
    severity: null,
    severityLevel: null,
    totalFactorScore: null,
    equipmentriskLevel: null,
    isDeal: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    isDelete: null
  };
  proxy.resetForm("assessmentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备评估";
}



function openAdd() {
  dialogMode.value = 'add'
  dialogVisible.value = true
}

function openEdit(row) {
  currentRow.value = row
  dialogMode.value = 'edit'
  dialogVisible.value = true
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getAssessment(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备评估";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["assessmentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAssessment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAssessment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除设备评估编号为"' + _ids + '"的数据项？').then(function() {
    return delAssessment(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sbtz/assessment/export', {
    ...queryParams.value
  }, `assessment_${new Date().getTime()}.xlsx`)
}

getList();
</script>
